﻿using System;
using System.Web;
using _5E.Infrastructure.Logging;
using _5E.Infrastructure.UnitOfWork;
using _5E.NHibernateRepository;
using _5E.WebInfrastructure.Security;
using Melee.Contract;
using Melee.Controller.Handlers;
using Melee.Controller.Request;
using Melee.Controller.Storage;
using Melee.Infrastructure.Configuration;
using Melee.Models.Repository;
using Melee.Repository;
using Melee.Service.Services;
using StructureMap;
using StructureMap.Configuration.DSL;

namespace Melee.WebUI
{
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            ObjectFactory.Initialize(x => x.AddRegistry<StructureMapRegistry>());

            ApplicationSettingsFactory.InitializeApplicationSettingsFactory(ObjectFactory.GetInstance<IApplicationSettings>());
            LoggingFactory.InitializeLogFactory(ObjectFactory.GetInstance<ILogger>());
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            CustomFormsAuthentication<UserCookie>.SetUserInfo(app.Context);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //Exception ex = Server.GetLastError();
            //StringBuilder log = new StringBuilder()
            //    .Append("Message:").AppendLine()
            //    .AppendFormat("    {0}", ex.Message).AppendLine()
            //    .Append("Source:").AppendLine()
            //    .AppendFormat("    {0}", ex.Source).AppendLine()
            //    .Append("StackTrace:").AppendLine()
            //    .Append(ex.StackTrace);
            //LoggingFactory.Logger.Log(log.ToString());
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }

    class StructureMapRegistry : Registry
    {
        public StructureMapRegistry()
        {
            //repository
            For<IAccountRepository>().Use<AccountRepository>();
            For<IAreaRepository>().Use<AreaRepository>();
            For<ICityRepository>().Use<CityRepository>();
            For<IDuplicateRepository>().Use<DuplicateRepository>();
            For<IMapRepository>().Use<MapRepository>();
            For<IPlayerRepository>().Use<PlayerRepository>();

            //service
            For<ICityService>().Use<CityService>();
            For<IDuplicateService>().Use<DuplicateService>();
            For<IGameService>().Use<GameService>();
            For<IMapService>().Use<MapService>();
            For<IUserService>().Use<UserService>();

            //其他
            For<IApplicationSettings>().Use<WebConfigApplicationSettings>();
            For<IHandlerFactory>().Use<HandlerFactory>();
            For<ILogger>().Use<Log4NetAdapter>();
            For<IUnitOfWork>().Use<NHibernateUnitOfWork>();
            For<IWebRequestFactory>().Use<WebRequestFactory>();
        }
    }
}